package com.lfy.modules.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfy.modules.blog.domain.ArticleClassificationEntity;
import com.lfy.modules.blog.domain.vo.article.ClassificationListVO;
import com.lfy.modules.blog.service.ArticleClassificationService;
import com.lfy.modules.blog.mapper.BlogArticleClassificationMapper;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.domain.entity.UserEntity;
import com.lfy.modules.common.intercept.AuthHandlerInterceptor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 吕飞扬
* @description 针对表【blog_article_classification】的数据库操作Service实现
* @createDate 2024-05-02 16:28:58
*/
@Service
public class ArticleClassificationServiceImpl extends ServiceImpl<BlogArticleClassificationMapper, ArticleClassificationEntity>
    implements ArticleClassificationService {

    @Override
    public R<List<ClassificationListVO>> getCategories() {
        UserEntity userEntity = AuthHandlerInterceptor.authLocal.get();
        List<ArticleClassificationEntity> list = this.list(new LambdaQueryWrapper<ArticleClassificationEntity>().eq(ArticleClassificationEntity::getTenantId, userEntity.getId()));
        if(CollectionUtils.isEmpty(list)){
            return R.success();
        }

        return R.success(list.stream().map(item -> {
            ClassificationListVO classificationListVO = new ClassificationListVO();
            BeanUtils.copyProperties(item, classificationListVO);
            return classificationListVO;
        }).collect(Collectors.toList()));
    }

    @Override
    public R<Void> addCategory(String categoryName) {
        UserEntity userEntity = AuthHandlerInterceptor.authLocal.get();
        ArticleClassificationEntity articleClassificationEntity = new ArticleClassificationEntity();
        articleClassificationEntity.setClassificationName(categoryName);
        articleClassificationEntity.setTenantId(userEntity.getId());
        this.save(articleClassificationEntity);
        return R.success();

    }

    @Override
    public R<Void> deleteCategory(Long categoryId) {
        UserEntity userEntity = AuthHandlerInterceptor.authLocal.get();
        this.remove(new LambdaQueryWrapper<ArticleClassificationEntity>().eq(ArticleClassificationEntity::getId, categoryId).eq(ArticleClassificationEntity::getTenantId, userEntity.getId()));
        return R.success();
    }

}




